package ptumall.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import ptumall.utils.JWTUtils;

import java.util.Collections;

@RestController
@RequestMapping("/api")
public class TokenController {

    @Autowired
    private JWTUtils jwtUtil;

    @GetMapping("/check-token")
    public ResponseEntity<?> checkToken(@RequestHeader("Authorization") String token) {
        if (token != null && token.startsWith("Bearer ")) {
            token = token.substring(7); // 去掉 "Bearer " 前缀
            boolean isValid = jwtUtil.validateToken(token); // 验证 Token 是否有效
            return ResponseEntity.ok(Collections.singletonMap("valid", isValid));
        }
        return ResponseEntity.badRequest().body("Token 无效");
    }
}
